package com.race.common.returntype;

import com.race.common.constant.ResponseEnums;

import java.io.Serializable;

/**
 * 全局返回数据统一封装
 * extends HashMap<String, Object> 也可以使用map数据结构
 * @author xc
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = -1908299815279646167L;
    private Integer code;
    private String desc;
    private T data;

    /**
     * 默认返回成功
     */
    public R(){
        this.code = ResponseEnums.SUCCESS.getCode();
        this.desc = ResponseEnums.SUCCESS.getDesc();
    }

    public R(Integer code) {
        this.code = code;
    }

    public R(String desc) {
        this.desc = desc;
    }

    public R(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public R(T data) {
        this.data = data;
    }

    public R(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public R(Integer code, String desc, T data) {
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> R<T> ok(){
        return new R<>();
    }

    public static <T> R<T> ok(String desc){
        return new R(ResponseEnums.SUCCESS.getCode(),desc);
    }

    public static <T> R<T> ok(Integer code,String desc){
        return new R(code,desc);
    }

    public static <T> R<T> ok(Integer code,String desc,T data){
        return new R(code,desc,data);
    }
    public static <T> R<T> ok(Integer code,T data){
        return new R(code,data);
    }
    public static <T> R<T> ok(String desc,T data){
        return new R(ResponseEnums.SUCCESS.getCode(),desc,data);
    }

    public static <T> R<T> error(){
        return new R(ResponseEnums.FAILURE.getCode(),ResponseEnums.FAILURE.getDesc());
    }
    public static <T> R<T> error(Integer code){
        return new R(code,ResponseEnums.FAILURE.getDesc());
    }
    public static <T> R<T> error(String desc){
        return new R(ResponseEnums.FAILURE.getCode(),desc);
    }
    public static <T> R<T> error(Integer code,String desc){
        return new R(code,desc);
    }
    public static <T> R<T> error(Integer code,T data){
        return new R(code,ResponseEnums.FAILURE.getDesc(),data);
    }
    public static <T> R<T> error(String desc,T data){
        return new R(ResponseEnums.FAILURE.getCode(),desc,data);
    }
    public static <T> R<T> error(Integer code,String desc,T data){
        return new R(code,desc,data);
    }

}
